//
//  ZLEmojiInputView.swift
//  Sina
//
//  Created by 朱玉龙 on 16/10/8.
//  Copyright © 2016年 朱玉龙. All rights reserved.
//

import UIKit
let emojiCellId = "emojiCellId"
class ZLEmojiInputView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
  
        setupUI()
        
//       let btn  = emojiToolbar.viewWithTag(2) as! UIButton
//        btn.isHidden = true
//        emojiToolbar.selectedBtn = btn
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI() {
       
        addSubview(emojiToolbar)
        addSubview(emojiCollecionView)
        
        emojiToolbar.snp.makeConstraints { (make) in
            make.bottom.leading.trailing.equalTo(self)
            make.height.equalTo(emojiToolbarH)
        }
        
        emojiCollecionView.snp.makeConstraints { (make) in
            make.top.trailing.leading.equalTo(self)
            make.bottom.equalTo(emojiToolbar.snp.top)
        }
        
        emojiToolbar.ItemBlock = { indexPath in
            
            self.emojiCollecionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
        }
    }
    
    //MARK: 图片
    lazy var emojiCollecionView: UICollectionView = {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSize(width: screenWidth, height: CGFloat(emojiInputViewH - emojiToolbarH))
        flowLayout.minimumLineSpacing = 0
        flowLayout.scrollDirection = .horizontal
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.isPagingEnabled = true
        collectionView.bounces = false
        collectionView.register(ZLEmojiCell.self, forCellWithReuseIdentifier: emojiCellId)
        
        return collectionView
    }()
    
    //MARK: 表情toolbar
    lazy var emojiToolbar: ZLEmojiToolbar = ZLEmojiToolbar()
    
}

extension ZLEmojiInputView: UICollectionViewDataSource,UICollectionViewDelegate {
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: emojiCellId, for: indexPath) as! ZLEmojiCell
        cell.backgroundColor = randomColor()
        cell.indexPath = indexPath
        return cell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let pointX = self.emojiCollecionView.contentOffset.x + screenWidth / 2
        
        let indexPath = emojiCollecionView.indexPathForItem(at: CGPoint(x: pointX, y: 0))
        
        print(indexPath)
        
       self.emojiToolbar.getChoiceItem(indexPath: indexPath!)
    }
}



